﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using RJ.Objects.GameEngine;
using System.Diagnostics;
using RJ.Euchre.App.Game;

namespace RJ.Euchre.App
{
    public partial class GameWnd : Form
    {
        private const int CLIENT_WIDTH = 1200;
        private const int CLIENT_HEIGHT = 700;

        private Stopwatch _timer = new Stopwatch();
        private double _lastTime;
        private long _frameCounter;
        private GameState gameState;
        private double _startTime;

        public GameWnd()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);

            gameState = new EuchreGameState(ClientSize);

            Initialize();
        }

        private void Initialize()
        {
            gameState.Initialize();

            //Initialise and start the timer
            _lastTime = 0.0;
            _timer.Reset();
            _timer.Start();
            _startTime = _timer.ElapsedMilliseconds;
        }

        private void GameWnd_FormClosed(object sender, FormClosedEventArgs e)
        {
            gameState.Dispose();
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAsAttribute(UnmanagedType.Bool)]
        static extern bool AllocConsole();

        private void GameWnd_Load(object sender, EventArgs e)
        {
            AllocConsole();
        }

        private void GameWnd_MouseClick(object sender, MouseEventArgs e)
        {
            gameState.MouseClick(sender, e);
        }

        private void GameWnd_MouseMove(object sender, MouseEventArgs e)
        {
            
        }

        private void GameWnd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
                gameState.Pause();
            else
                gameState.KeyboardKeyPressed(e.KeyCode);
        }

        private bool writeFps = false;
        private Brush fpsBrush = new SolidBrush(Color.DarkRed);
        private Font fpsFont = new Font("Arial", 12);
        private long frameCounterWrite = 0;

        private void GameWnd_Paint(object sender, PaintEventArgs e)
        {
            //Work out how long since we were last here in seconds
            double gameTime = _timer.ElapsedMilliseconds / 1000.0;
            double elapsedTime = gameTime - _lastTime;
            _lastTime = gameTime;
            _frameCounter++;


            //Perform any animation and updates
            if (MouseButtons == MouseButtons.Left || MouseButtons == MouseButtons.Right)
                Console.WriteLine("{0} MouseButton pressed;", MouseButtons);
            gameState.Update(gameTime, elapsedTime);


            //Draw everything
            gameState.Draw(e.Graphics);
            var mousePos = PointToClient(MousePosition);
            e.Graphics.DrawString(string.Format("FPS: {0}", frameCounterWrite), fpsFont, fpsBrush, new PointF(0, 150));
            e.Graphics.DrawString(string.Format("Mouse: {0}, {1}", mousePos.X, mousePos.Y), fpsFont, fpsBrush, new PointF(0, 165));

            if (_timer.ElapsedMilliseconds >= _startTime + 1000)
            {
                frameCounterWrite = _frameCounter;
                _frameCounter = 0;
                _startTime = _timer.ElapsedMilliseconds;
            }
            //Force the next Paint()
            this.Invalidate();
        }
    }
}
